package com.atguigu.baseservice.handler.exception;

import com.atguigu.baseservice.result.ResultCodeEnum;
import com.atguigu.baseservice.result.R;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 统一异常处理类
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);


    //处理自定义异常
    @ExceptionHandler(value = GuliException.class)
    public R baseExceptionHandler(GuliException e){
        logger.error("发生业务异常！原因是：" + e.getMsg());
        return R.error(e.getCode(), e.getMsg());
    }

//    //处理空指针异常
//    @ExceptionHandler(value = NullPointerException.class)
//    public R exceptionHandler(NullPointerException e){
//        logger.error("发生空指针异常！原因是：" + e);
//        e.printStackTrace();
//        return R.error(ResultCodeEnum.BODY_NOT_MATCH);
//    }

    //处理其他异常
    @ExceptionHandler(value = Exception.class)
    public R exceptionHandler(Exception e){
        logger.error("未知异常！原因是：" + e);
        e.printStackTrace();
        return R.error(ResultCodeEnum.INTERNAL_SERVER_ERROR);
    }

}
